
import { _polyline, _arcTo } from "./curve.js"
import { _path, _g, _text } from "./svg.js"
import { markInfo } from '../algorithm/mark.js'
export const _mark = ({ points, mark }, props) => {
    let mi = markInfo(points, mark)
    let { angle = true, edge = true, radius = false, labels = false, boundingClientRect = false } = mark

    let g = []
    if (angle) {
        let { as } = mi
        as.forEach(({ points, a, ia, sweepFlag, o, r, label, labelPos }) => {
            let path = _polyline({ points })

            // 凹多边形（Concave Polygon
            // 优角（Reflexive Angle）   sweepFlag==1  顺时针

            path += _arcTo({
                r,
                to: points[0],
                sweepFlag: sweepFlag === 1
            })

            g[g.length] = _path(path, { ...props, strokeDasharray: 0 })
            g[g.length] = _text({
                x: labelPos[0],
                y: labelPos[1],
                text: label
            }, { fontSize: 10, ...props, strokeDasharray: 0, textAnchor: 'middle' })
        })

    }

    if (edge) {
        let { es } = mi

        es.forEach(({ points, label, labelPos }) => {
            g[g.length] = _path(_polyline({ points }), { strokeDasharray: 4, ...props, fill: 'none' })

            g[g.length] = _text({
                x: labelPos[0],
                y: labelPos[1],
                text: label
            }, { fontSize: 10, ...props, strokeDasharray: 0, textAnchor: 'middle' })
        })

    }

    if (radius) {
        let { rs } = mi
        rs.forEach(({ points, label, labelPos }) => {
            g[g.length] = _path(_polyline({ points }), { strokeDasharray: 4, ...props, fill: 'none' })

            g[g.length] = _text({
                x: labelPos[0],
                y: labelPos[1],
                text: label
            }, { fontSize: 10, ...props, strokeDasharray: 0, textAnchor: 'middle' })
        })
    }

    if (labels) {
        let { ls } = mi
        ls.forEach(({ label, labelPos }) => {
            g[g.length] = _text({
                x: labelPos[0],
                y: labelPos[1],
                text: label
            }, { fontSize: 10, ...props, strokeDasharray: 0, textAnchor: 'middle' })
        })
    }

    if (boundingClientRect) {
        let { bs } = mi
        bs.forEach(({ points, label, labelPos }) => {
            g[g.length] = _path(_polyline({ points }), { strokeDasharray: 4, ...props, fill: 'none' })

            g[g.length] = _text({
                x: labelPos[0],
                y: labelPos[1],
                text: label
            }, { fontSize: 10, ...props, strokeDasharray: 0, textAnchor: 'middle' })
        })
    }

    return _g(g, {
        name: 'mark'
    })
}